<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController {

	/**
	 * [index 显示用户信息]
	 */
	public function index(){
		$user_id = $_SESSION['home']['uid'];
		$where['user_id'] = $user_id;
		$user_info = M('user_info');
		$res = $user_info->where($where)->find();

		// 计算年龄
		$res['age'] = floor((time() - $res['birthday'])/(365*24*60*60));
		$res['birthday'] = transfBirth($res['birthday'],false);

		//计算得到用户等级
		$grade = floor( sqrt( $res['exp'] ) );

		$this->assign('test', '个人资料');
		$this->assign('res', $res);
		$this->assign('grade', $grade);

		$user = M('user');
		$u_res = $user->field('regtime,regip,lasttime,lastip')->where($where)->find();
		$this->assign('u_res', $u_res);

		$pug=M('Pug');
		$pugList=$pug->query('select user_id,nickname,photo,time
 from zz_pug left join zz_user_info on zz_pug.user_id_a=zz_user_info.user_id where user_id_b='.$user_id.' order by `time` desc');
		$this->assign('pugList',$pugList);

		$this->display();


	}

	//用户信息编辑页面
	public function edit(){
		$id = $_SESSION['home']['uid'];
		$user = M('user');
		$res = $user->alias('u')->join('zz_user_info ui on u.user_id=ui.user_id')->where("u.user_id='$id'")->select();
		$res[0]['birthday'] = transfBirth($res[0]['birthday'], false);
		$this->assign('res', $res);

		$this->assign('test', '编辑信息');

		$this->display();
	}

	//用户信息编辑处理页面   因为要上传头像  没有使用ajax
	public function editdo(){

		// if (empty($_POST['nickname'])) {
		// 	$this->error('名字(昵称)不能为空');
		// }

		// 用昵称不能为空验证
		if($_POST['nickname']==''){
			$this->error('昵称不能为空');
			exit;
		}

		if ($_FILES['photo']['error'] == 0) {
			$str = Uploads('photo');
			if ($str) {
				$_POST['photo'] = $str;
			}
		}

		// 处理表单数据
		$birthday = I('post.birthday');
		$_POST['birthday'] = transfBirth($birthday);

		$userinfo = M('user_info');
		$userinfo->create();
		$res = $userinfo->save();

		if (!$res) {
			$this->error('修改失败');
		} else {
			if ($_FILES['photo']['error'] == 0) {
				$_SESSION['home']['photo'] = $str;
			}

			if ($_POST['nickname'] != $_SESSION['home']['nickname']) {
				$_SESSION['home']['nickname'] = $_POST['nickname'];
			}
			$this->success('修改成功', U('index'));
		}
	}


	//当用户签到的时候调用的ajax
	public function ajaxexp(){
		if (IS_AJAX) {
			//判断当前用户是否可以签到
			$user_info = M('user_info');
			//查询上次签到的时间
			$res = $user_info -> field('zz_user_info.sign,zz_user_info.vipday') -> where('user_id='.$_SESSION['home']['uid']) -> select();
			$sign = $res[0]['sign'];
			$vipday = $res[0]['vipday'];
			if ( $sign + 3600*24 < time() ) {
				//生成一个随即经验值
				if ($vipday > time()) {
					//当是会员的时候
					$expnum = mt_rand(20,40);
				}else{
					//当不是会员的时候
					$expnum = mt_rand(10,20);
				}
				//当前的时间
				$data['sign'] = time();
				$sign1 = $user_info -> where('user_id='.$_SESSION['home']['uid']) -> setInc('exp',$expnum);
				$sign2 = $user_info -> where('user_id='.$_SESSION['home']['uid']) -> save($data);
				if ($sign1 and $sign2) {
					echo $expnum;
				}else{
					echo 'no';
				}
			}else{
				echo 'no';
			}
		}else{
			$this -> error('非法操作');
		}
	}

	//人脸特征码存储ajax
	public function savefeature(){
		if(IS_AJAX){
			$user_id=$_SESSION['home']['uid'];
			$data['feature']=I('post.feature','','string');
			if($data['feature']!=''){
				$user=M('User');
				$user->where('user_id="'.$user_id.'"')->save($data);
			}
		}
	}

	/**
	 * [myPage 个人主页]
	 */
	public function myPage(){
		$user = M('user');
		$user_id = $_SESSION['home']['uid'];
		// $userinfo->where("user_id='$user_id'")->select();
		$userreg = $user->field('regtime')->find($user_id);
		// var_dump($user);
		$ryear = date('Y',$userreg['regtime']);
		// echo $byear;
		// $rytime = mktime(0,0,0,1,1,$ryear);
		$nytime = date('Y')+1;
		$arr = array();
		$brr = array();
		for ($i=$nytime ; $i >= $ryear; $i--) {
			// echo '<hr>';
			// echo $i;
			$start = mktime(0,0,0,1,1,$i);
			$end = mktime(0,0,0,1,1,$i+1);
			$m = M();
			// 查询news和photo信息, 判断时间
			$sql = "select * from (
			select n1.news_id,n1.user_id, n1.time n1time, n1.content,n1.turn_content,n1.isturn,n1.pt
			from zz_news n1 join zz_news n2
			on n1.isturn=n2.news_id
			where n1.user_id=$user_id
			union all
			select p.photos_id,a.user_id,p.time n1time, p.photos_path, a.albums_name,p.photos_info,p.pt
			from zz_photos p join zz_albums a
			on p.albums_id=a.albums_id
			where a.user_id=$user_id ) t  where n1time between $start and $end order by n1time";
			// $sql = "select * from zhong1 where n1time between $start and $end ";
			$res = $m->query($sql);
			// var_dump($res);
			if ($res) {
				for ($j=12; $j > 0; $j--) {
					$startm = mktime(0,0,0,$j,1,$i);
					$endm = mktime(0,0,0,$j+1,1,$i);

					$sql = "select * from (
						select n1.news_id,n1.user_id, n1.time n1time, n1.content,n1.turn_content,n1.isturn,n1.pt,ui.nickname,n3.user_id n3user_id
						from zz_news n1 join zz_news n2
						on n1.isturn=n2.news_id
						join zz_news n3 on n1.isturn=n3.news_id
						join zz_user_info ui on n3.user_id= ui.user_id
						where n1.user_id=$user_id
						union all
						select p.photos_id,a.user_id,p.time n1time, p.photos_path, a.albums_name,p.photos_info,p.pt,a.albums_id,a.albums_id i4
						from zz_photos p join zz_albums a
						on p.albums_id=a.albums_id
						where a.user_id=$user_id ) t  where n1time between $startm and $endm order by n1time desc";
					$resm = $m->query($sql);
					if ($resm) {
						// 把信息压入数组
						$arr[$i.'-'.$j][] = $resm;
						$brr[$i][] = $j;

					}

				}
			}

		}

		$this->assign('arr',$arr);
		$this->assign('brr',$brr);

		// 拼接js代码, 滚动监听效果
		$html = '<script>$(function(){';
		foreach($brr as $ky => $ve){
			foreach($ve as $ki=>$vm){
$html .= <<<HELLO
				var waypoint = new Waypoint({
				  element: $('#m{$ky}{$vm}'),
				  handler: function(direction) {
					if (direction=='down') {
					   	$('.m{$ky}{$vm}').parents('ol').show();
						$('.m{$ky}{$vm}').addClass('current').parents('li').addClass('current');

						$('.m{$ky}{$vm}').show().siblings().removeClass('current').show();
						$('.m{$ky}{$vm}').parents('li').siblings().removeClass('current').find('ol').hide();
					}else{


						if ($('.m{$ky}{$vm}').prev().length==1) {
							   	$('.m{$ky}{$vm}').prev().parents('ol').show();
								$('.m{$ky}{$vm}').prev().addClass('current').parents('li').addClass('current');

								$('.m{$ky}{$vm}').prev().show().siblings().removeClass('current').show();
								$('.m{$ky}{$vm}').prev().parents('li').siblings().removeClass('current').find('ol').hide();
						} else {
							$('.m{$ky}{$vm}').parents('ol').hide().parents('li').removeClass('current').prev().addClass('current').find('ol').show().children('li:last').addClass('current').siblings().show();
						}

					}

				  },
				  offset:60
				});

HELLO;
			}
		}
		$html .= '})</script>';
		$this->assign('html',$html);
		// var_dump($html);
		// die;
		$userinfo = M('user_info');
		$bir = $userinfo->field('birthday')->find($user_id);
		$birth = date('Y-m-d',$bir['birthday']);
		$this->assign('birth',$birth);
		$this->assign('test','时间轴');
		$this->display();
	}

}
